
const getFarmList = async ctx=>{
  // let {farmId} = ctx.query;

  // 1. 所有的农事 
  // 2. 每个农事 关联每一个农资
  // 3. 任务反馈

  let farm = await ctx.db.select('farm');
  // farm 单纯的是农事的数据  
  // [{am:[],rw:['','']}]

  if(farm.length!==0){// await  报错 
    let result = farm.map(async item=>{ // 洋葱圈模型
      return await ctx.db.execute(`select farm.farmName,am.amName,am.amId,am.amType
      from farm_am
      LEFT JOIN farm ON farm.farmId = farm_am.faId
      LEFT JOIN am ON farm_am.amId = am.amId
      where farm.farmId=${item.farmId};`);
    });
    // console.log(result);//数组 [{},[],promise]

    let amList = await Promise.all(result); // 处理promise的数组  批量处理promise
    console.log(amList);

    farm.forEach((element,index) => {
      amList.length!==0?element['am'] = amList[index]:[];
    });


    // let result = await ctx.db.execute(`select farm.farmName,am.amName,am.amId,am.amType
    // from farm_am
    // LEFT JOIN farm ON farm.farmId = farm_am.faId
    // LEFT JOIN am ON farm_am.amId = am.amId
    // where farm.farmId=1;`);// []  {}  数值  promise
  
    // console.log(result);
  
    ctx.body = farm;
  }
};

module.exports = {
  getFarmList
};